package de.jmda.mview.fx.node.typeshape;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javafx.geometry.HPos;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;

import de.jmda.mproc.ProcessingUtilities;
import de.jmda.mview.typeshape.DisplayOptionsModelUtil;

public class Fields
    extends VBox
{
//	private final static Logger LOGGER = Logger.getLogger(Fields.class);
//
//	private TypeElement typeElement;
//	private DisplayOptionsModelUtil displayOptionsModelUtil;
	private Typeshape typeshape;
	
	private Label header;

	private Map<String, FieldView> fieldViewMap = new HashMap<String, FieldView>();

	/**
	 * sorted set of fields
	 */
	private Set<ElementData<VariableElement>> fieldDataSet =
			new TreeSet<ElementData<VariableElement>>(
					new Comparator<ElementData<VariableElement>>()
					{
						@Override
		        public int compare(
		        		ElementData<VariableElement> o1,
		        		ElementData<VariableElement> o2)
		        {
			        return o1.getName().compareTo(o2.getName());
		        }
					});

	public Fields(
			TypeElement typeElement, DisplayOptionsModelUtil displayOptionsModelUtil,
			Typeshape typeshape)
	{
		super();

		if (typeshape == null)
		{
			throw new IllegalArgumentException("typeshape must not be null");
		}

//		this.typeElement = typeElement;
//		this.displayOptionsModelUtil = displayOptionsModelUtil;
		this.typeshape = typeshape;

		for (VariableElement field : ProcessingUtilities.getFields(typeElement))
		{
			ElementData<VariableElement> fieldData =
					new ElementData<VariableElement>(field, displayOptionsModelUtil);
			FieldView fieldView =
					new FieldView(fieldData, displayOptionsModelUtil, typeshape);

			fieldDataSet.add(fieldData);
			fieldViewMap.put(field.toString(), fieldView);
		}
		
		GridPane gridPane = new GridPane();

		int i = 0;
		
		gridPane.setHgap(2);

		for (ElementData<VariableElement> fieldData : fieldDataSet)
		{
			FieldView fieldView = fieldViewMap.get(fieldData.getElement().toString());

			gridPane.add(fieldView.getIcon(), 0, i);
			gridPane.add(fieldView.getType(), 1, i);
			gridPane.add(fieldView.getName(), 2, i);

			i++;
		}
		
		ColumnConstraints columnConstraints = new ColumnConstraints();
		columnConstraints.setHalignment(HPos.RIGHT);
		
		gridPane.getColumnConstraints().add(0, columnConstraints);

		header = new Label("fields");
		header.setMaxWidth(Double.MAX_VALUE); // makes header span the whole width of typeshape (vbox)
		header.getStyleClass().add("fields-header");
		
		getChildren().add(header);
		getChildren().add(gridPane);
	}

	public FieldView getFieldView(VariableElement field)
  {
	  return fieldViewMap.get(field.toString());
  }
}